package com.ll.controller;

import com.ll.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 2020/9/13 - 10:24
 */
@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * 返回字符串
     * @param model
     * @return
     */
    @RequestMapping("/testString")
    public String testString(Model model){
        System.out.println("UserController => testString()执行了");
        // 模拟从数据库中查询出数据库对象
        User user = new User();
        user.setUsername("刘龙");
        user.setPassword("123");
        user.setAge(30);

        //model对象
        //底层会存储到request域对象中
        model.addAttribute("user", user);

        return "success";
    }

    /**
     * 没有返回值
     * 请求转发: 一次请求,不用编写项目的名称
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/testVoid")
    public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("UserController => testVoid()执行了");

        //编写请求转发
        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);

        return;
    }

    /**
     * 重定向
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/testVoid2")
    public void testVoid2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("UserController => testVoid2()执行了");
        System.out.println(request.getContextPath());

        //重定向 两次请求
        response.sendRedirect(request.getContextPath() + "/index.jsp");

        return;
    }

    @RequestMapping("/testVoid3")
    public void testVoid3(HttpServletResponse response) throws IOException {
        System.out.println("UserController => testVoid3()执行了");

        //设置中文乱码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        response.getWriter().println("刘龙");

        return;
    }

    /**
     * 返回 ModelAndView
     * @return
     */
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        //创建ModelAndView对象
        ModelAndView modelAndView = new ModelAndView();

        System.out.println("UserController => testModelAndView()执行了");

        // 模拟从数据库中查询出数据库对象
        User user = new User();
        user.setUsername("赵海阳");
        user.setPassword("456");
        user.setAge(30);

        //把user对象存储到modelAndView对象中,也会把user对象存入到request对象
        modelAndView.addObject("user", user);

        //跳转到哪个页面
        modelAndView.setViewName("success");

        return modelAndView;
    }

    /**
     * 使用关键字的方式进行转发或者重定向
     * @return
     */
    @RequestMapping("/testForwardOrRedirect")
    public String testForwardOrRedirect(){
        System.out.println("UserController => testForwardOrRedirect()执行了");

//        //请求转发
//        return "forward:/WEB-INF/pages/success.jsp";

        //重定向
        return "redirect:/index.jsp";
    }

    /**
     * @ResponseBody User 将User对象转换为json
     * 模拟异步请求响应
     */
    @RequestMapping("/testAjax")
    public @ResponseBody User testAjax(@RequestBody User user){
        System.out.println("UserController => testAjax()执行了");
        //客户端发送ajax的请求,传的是json字符串,后端把json字符串封装到user对象中
        System.out.println(user);

        //做响应,模拟查询数据库
        user.setUsername("赵海阳");
        user.setAge(40);

        //做响应
        return user;
    }

    @RequestMapping("/testAjax2")
    public @ResponseBody User testAjax2(@RequestBody User user){
        System.out.println("UserController => testAjax()2执行了");
        //客户端发送ajax的请求,传的是json字符串,后端把json字符串封装到user对象中
        System.out.println(user);

        //做响应,模拟查询数据库
        user.setUsername("赵海阳");
        user.setAge(22);

        //做响应
        return user;
    }



}
